<%--
  Created by IntelliJ IDEA.
  User: yangwu
  Date: 2021/8/30
  Time: 17:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!--引入css支持-->
    <link rel="stylesheet" href="/statics/css/bootstrap.css" />

    <!--如果要引用js要先引入支持-->
    <script type="text/javascript" src="/statics/js/jquery-2.1.0.min.js"></script>
</head>
<body>
    <div class="container" style="margin-top: 80px;">
        <div class="col-md-2">

        </div>
        <div class="col-md-8">
            <div class="row" style="padding-bottom: 10px;">
                <h4>添加销售</h4>
            </div>
            <div></div>
            <form action="/sale/add" id="saleForm">
                <div class="form-inline">
                    <label for="productId">商品名称:</label>
                    <select name="productId" id="productId">
                        <option value="0">===请选择===</option>
                    </select>
                </div><br>
                <div class="form-inline">
                    <label for="price">销售单价:</label>
                    <input type="text" class="form-control" id="price" name="price">
                </div><br>
                <div class="form-inline">
                    <label for="quantity">销售数量:</label>
                    <input type="text" class="form-control" id="quantity" name="quantity">
                </div><br>
                <div class="form-inline">${error}</div>
                <button type="button" class="btn btn-default" onclick="formSubmit()">保存</button>
            </form>
        </div>
    </div>
    <script>
        $(function (){
            $.ajax({
                url:"/product/list",
                success:function (data){
                    var options="";
                    for(var i=0;i<data.length;i++){
                        options+="<option value='"+data[i].id+"'>"+data[i].productName+"</option>"
                    }
                    $("#productId").append(options);
                }
            })
        })
        function formSubmit(){
            var productId=$("#productId").val();
            var price=$("#price").val();
            var quantity=$("#quantity").val();
            if(productId==0||price==""||quantity==""){
                alert("请填完整销售信息！")
                return;
            }
            if(isNaN(price)||isNaN(quantity)||price<0||price<0){
                alert("销售单价和销售数量必须是非负数字！")
                return;
            }
            $("#saleForm").submit();
        }
    </script>
</body>
</html>
